package com.example.liunanxin.lnx226ordering.model;




import com.example.liunanxin.lnx226ordering.beans.FoodBean;
import com.example.liunanxin.lnx226ordering.iface.FoodDetailListener;
import com.example.liunanxin.lnx226ordering.iface.FoodDetailiface;
import com.example.liunanxin.lnx226ordering.retrofitservice.FoodService;
import com.example.liunanxin.lnx226ordering.url.Url;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodDetailModel implements FoodDetailiface {

    private Retrofit retrofit;

    public FoodDetailModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Url.USERURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getFoodById(int food_id, final FoodDetailListener foodDetailListener) {
        final FoodService foodService=retrofit.create(FoodService.class);
        Call<FoodBean> call=foodService.getFoodById(food_id);
        call.enqueue(new Callback<FoodBean>() {
            @Override
            public void onResponse(Call<FoodBean> call, Response<FoodBean> response) {
                if(response.isSuccessful()&&response!=null)
                    foodDetailListener.onResponse(response.body());
                else
                    foodDetailListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<FoodBean> call, Throwable t) {
                foodDetailListener.onFail(t.toString());
            }
        });
    }

}
